package com.dld.coupon.view;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dld.coupon.activity.ActivityManager;
import com.dld.coupon.activity.R;
import com.dld.coupon.activity.SearchResultActivity;
import com.dld.coupon.util.Common;
import com.dld.coupon.util.StringUtils;

public class UMSearchBar extends LinearLayout {
	private ArrayAdapter<String> adapter;
	private OnSearchListener defaultClickListener = new OnSearchListener() {
		public void onSearch(int type, String keyword) {

			if (keyword != null && (keyword.length() > 0)) {
				Intent it = new Intent(getContext(), SearchResultActivity.class);
				// type 0 1 都指向 --> coupon ,2 指向 store
				it.putExtra("type", type);
				it.putExtra("keyword", keyword);
				getContext().startActivity(it);
			}else{
				showWarningMessage();
			}
		}
	};

	private void showWarningMessage() {
		AlertDialog.Builder adb = new AlertDialog.Builder(getContext());
		AlertDialog ad = adb.create();
		ad.setTitle("店连店");
		ad.setMessage("请输入搜索的关键词");
		ad.setButton("确定", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {

			}

		});
		ad.show();

	}

	private int position = 0;
	private AutoCompleteTextView searchField;
	private OnSearchListener searchListener;

	public UMSearchBar(Context paramContext, AttributeSet paramAttributeSet) {
		super(paramContext, paramAttributeSet);
	}

	private void init() {
		this.position = 0;
		this.searchField = ((AutoCompleteTextView) findViewById(R.id.main_input_search));
		this.adapter = new ArrayAdapter(getContext(), R.layout.dropdown_item);
		this.searchField.setAdapter(this.adapter);
		this.searchField.setThreshold(0);
		final TextView localTextView = (TextView) findViewById(R.id.search_spinner);
		localTextView.setText("打折");
		localTextView.setOnClickListener(new View.OnClickListener() {
			public void onClick(View paramView) {
				DialogInterface.OnClickListener local1 = new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface paramDialogInterface,
							int paramInt) {

						paramDialogInterface.dismiss();
						UMSearchBar.this.position = paramInt;
						String[] arrayOfString = ActivityManager.getCurrent()
								.getResources().getStringArray(
										R.array.search_types);
						// searchField
						// .setText(arrayOfString[UMSearchBar.this.position]);
						localTextView
								.setText(arrayOfString[UMSearchBar.this.position]);

					}
				};

				AlertDialog.Builder builder = new AlertDialog.Builder(
						UMSearchBar.this.getContext());
				builder.setTitle("请选择搜索类型").setSingleChoiceItems(2131165186,
						UMSearchBar.this.position, local1).show();

			}
		});
		findViewById(R.id.search_button).setOnClickListener(
				new View.OnClickListener() {
					public void onClick(View paramView) {
						UMSearchBar.this.startSearch();
					}
				});
		this.searchField.setOnKeyListener(new View.OnKeyListener() {
			public boolean onKey(View paramView, int paramInt,
					KeyEvent paramKeyEvent) {
				boolean i = false;
				if ((paramKeyEvent.getAction() == 1) && (paramInt == 66)) {
					UMSearchBar.this.startSearch();
					i = true;
				}
				return i;
			}
		});
	}

	public String getKeyword() {
		return this.searchField.getEditableText().toString();
	}

	public int getPosition() {
		return this.position;
	}

	protected void onFinishInflate() {
		super.onFinishInflate();
		init();
	}

	public void setOnSearchListener(OnSearchListener paramOnSearchListener) {
		this.searchListener = paramOnSearchListener;
	}

	public void startSearch() {
		String str = this.searchField.getText().toString().trim();
		if (!StringUtils.isEmpty(str)) {
			if (this.searchListener == null) {
				this.searchListener = this.defaultClickListener;
			} else {
				this.searchListener.onSearch(this.position, str);
			}
			Common.hideKeyboard(this);
		}else{
			showWarningMessage();
		}
	}

	public static abstract interface OnSearchListener {
		public abstract void onSearch(int type, String keyword);
	}
}
